﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using Omu.Awesome.Core;
using XPressify.Core;
using XPressify.Data.Helpers;

namespace XPressify.Data.CompliedQueries
{
    public class GroupCompiledQueries
    {


        public static Func<Repository, Guid, Group> GetGroup
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, Group>(
                    (context, id) => context.BaseSubjects.OfType<Group>().SingleOrDefault(gr => gr.Id == id)
                    );
            }
        }

        public static Func<Repository, Guid, bool> IsGroup
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, bool>(
                    (context, id) => context.BaseSubjects.OfType<Group>().Any(gr => gr.Id == id)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Group>> GetAllGroupsOf
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Group>>(
                    (context, userId) => context.BaseObjects.OfType<Relationship>().Where(
                        rel => rel.BaseSubject.Id == userId && rel.Status == RelationshipStatus.Confirmed).Select(
                        rel => rel.BaseSubject2).OfType<Group>()
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Group>> GetAllPublicGroupsOf
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Group>>(
                    (context, userId) => context.BaseObjects.OfType<Relationship>().Where(
                        rel => rel.BaseSubject.Id == userId && rel.Status == RelationshipStatus.Confirmed).Select(
                            rel => rel.BaseSubject2).OfType<Group>().Where(gr => gr.PrivacyType == SubjectPrivacy.Public)
                    );
            }
        }

    }
}